<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/10/26
 *app 首页视频
 * Time: 11:20
 */

namespace app\api\controller\Index;


use app\api\controller\AppController;
use app\api\model\Index\VideoUser;
use app\api\model\Index\VideoUserComment;
use app\Request;
use app\Service\CommentsService;
use app\Service\VideoService;
use app\Utils\Util;
use think\App;
use think\facade\Db;

class Video extends AppController
{

    protected $redis;

    /**
     * 视频发布/编辑
     */
    public function __construct(App $app)
    {
        parent::__construct($app);
        //  $this->redis = new Redis();
    }

    /**
     * @param Request $request
     * @param VideoService $service
     * $url 视频地址
     * $title 视频标题
     * 视频上传
     */
    public function saves(Request $request, VideoService $service)
    {
        $data = $request->param();
        $data['user_id'] = $this->user ? $this->user['id'] : 0;
        $info = $service->save($data);
        if ($info['code'] !== 1) {
            return Util::returnJson(self::CODE_ERROR, [], $info['msg']);
        }
        return Util::returnJson(self::CODE_SUCCESS, [], '上传成功');
    }

    /**
     * 首页视频列表
     * @param Request $request
     * @return array|\think\response\Json
     */
    public function videos(Request $request, VideoService $service)
    {
        list($limit, $page) = [
            $request->get('limit', 10),
            $request->get('page', 1),
        ];
        $limit = $limit > 1000 || $limit < 1 ? 10 : $limit;
        $uid = $this->user ? $this->user['id'] : 0; //用户id
        return Util::returnJson(self::CODE_SUCCESS, $service->video_lists($uid, $limit, $page), 'OK');
    }

    /**
     * 视频评论列表
     * @param Request $request
     * @return array|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function comments(Request $request, CommentsService $service)
    {
        list($video, $limit, $page) = [
            $request->param('video', 0),
            $request->param('limit', 10),
            $request->param('page', 1),
        ];
        $limit = $limit > 1000 || $limit < 1 ? 10 : $limit;
        $lists = $service->lists($video, $page, $limit);
        return Util::returnJson(self::CODE_SUCCESS, $lists, 'OK');
    }


    /**
     * $video_id 视频id
     * 获取视频的
     * 点赞数和评论数  关注状态
     * token 非必传参数
     */
    public function likes(Request $request, VideoService $service)
    {
        $video_id = (int)$request->param('video_id');
        if (empty($video_id)) return Util::returnJson(self::CODE_ERROR, [], '视频ID必传');
        $result = $service->likes($video_id, $this->user);
        return Util::returnJson(self::CODE_SUCCESS, $result, '获取成功');
    }

    /**
     * @param Request $request
     * @param VideoService $service
     * $video_id 视频ID
     * $is_show 状态  1正常 0下架
     * 视频上下架处理
     */
    public function video_is_show_lists(Request $request, VideoService $service)
    {
        $video_id = $request->param('video_id');
        $is_show = $request->param('is_show');
        $user_id = $this->user ? $this->user['id'] : 0;
        if (empty($video_id)) return Util::returnJson(self::CODE_ERROR, [], '缺少参数');
        $res = $service->video_is_show_lists($user_id, $video_id, $is_show);
        if ($res !== false) {
            return Util::returnJson(self::CODE_SUCCESS, [], '操作成功');
        }
        return Util::returnJson(self::CODE_ERROR, [], '操作失败');
    }
}